package com.jdym.controller;


import com.jdym.R;
import com.jdym.entity.Return;
import com.jdym.service.ReturnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 还款表 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-09-20
 */
@RestController
@CrossOrigin
@RequestMapping("/return")
public class ReturnController {
    @Autowired
    ReturnService returnService;

    @RequestMapping(value = "/getAllPutLoanReturn/{firmId}")
    public R getAllPutLoanReturn(@PathVariable int firmId){

       List<Return> re= returnService.getAllPutLoanReturn(firmId);
        Date a;
        Date b=new Date();//当前日期  2021 11
       for(int i=0;i<re.size();i++){
          a= re.get(i).getFinalDate();//截止日期  2021 12
           if(a.before(b)){
               System.out.println("true");//逾期1
               re.get(i).setOverDate(1);
           }else {
               System.out.println("false");//未逾期0
               re.get(i).setOverDate(0);
           }
       }
        return R.ok().data("re",re);
    }

    /**
     *
     * @param returnId  还款id
     * @param putMoney   还款金额
     * @return
     */
    @RequestMapping(value = "/confirmReturn/{returnId}")
    public R confirmReturn(@PathVariable int returnId, @RequestParam BigDecimal putMoney){

        Return re=returnService.selectReturnById(returnId);
        BigDecimal a= re.getReturnedAmount().add(putMoney);
        Return r=new Return();
        r.setReturnId(returnId);
        r.setReturnedAmount(a);
        returnService.updateReturnById(r);
        return R.ok();
    }

}

